package de.tu_darmstadt.gdi1.xcom.tests.testcases.tutors;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2;

/**
 * 
 * Tests if shooting-actions are performed correctly
 * 
 * @author Lukas Schwoebel
 *
 */
public class KITests {
	
	private XcomTestAdapterLevel2 testAdapter;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel2();
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2#performKIAction()}.
	 */
	@Test
	public final void testKIHandledCorrectly() {
		
		testAdapter.loadLevelFromString(TestLevel.saveGame1);
		
		String returnedSavegame = testAdapter.returnGamestateAsSavegame();
		
		// perform ki-action for non-existent alien
		try {
			testAdapter.performKIAction("2;7;f");
		}
		catch(Exception e) {
			fail("Performing a ki-action for a non-existent alien on the given position should not cause an exception");
		}
		assertEquals("A KI-action for non-existent aliens should not change the gamestate", returnedSavegame, testAdapter.returnGamestateAsSavegame());
		
		// perform ki-action for a human player
		try {
			testAdapter.performKIAction("1;4;r");
		}
		catch(Exception e) {
			fail("Performing a ki-action for a human player on the given position should not cause an exception");
		}
		assertEquals("A KI-action for a human player should not change the gamestate", returnedSavegame, testAdapter.returnGamestateAsSavegame());
		
		// an alien player should not be able to walk through walls on closed doors
		testAdapter.performKIAction("17;4;l");
		testAdapter.performKIAction("17;4;f");
		assertEquals("A alien should not be able to walk through a wall, so on 16:4 still should be a wall-element", '#', testAdapter.elementOnPosition(16, 4));
		assertEquals("A alien should not be able to walk through a wall, so the alien should still be on 17:4", 'Z', testAdapter.elementOnPosition(17, 4));
		assertTrue("Walking through a wall is not possible, so the alien still should have 74 actionpoints",
				(testAdapter.returnGamestateAsSavegame().indexOf("//Z;17;4;w;50;74;50;0;0") >= 0));

		// "attacking" is not possible either
		testAdapter.performKIAction("17;4;a");
		assertEquals("A alien should not be able to 'attack' a wall, so on 16:4 still should be a wall-element", '#', testAdapter.elementOnPosition(16, 4));
		assertEquals("A alien should not be able to 'attack' a wall, so the alien should still be on 17:4", 'Z', testAdapter.elementOnPosition(17, 4));
		assertTrue("Attacking a wall is not possible, so the alien still should have 74 actionpoints",
				(testAdapter.returnGamestateAsSavegame().indexOf("//Z;17;4;w;50;74;50;0;0") >= 0));
	}
}
